﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FillCartridgeBBB.Models;

namespace FillCartridgeBBB.Controllers
{
    public class CustomerController : Controller
    {
        private DataManager _dataManager;

        public CustomerController (DataManager datamanager)
        {
            _dataManager = datamanager;
        }

        public ActionResult List()
        {
            ViewData["Header"] = "Ремонтні організації";
            ViewData.Model = _dataManager.Customers.SelectAll();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
           return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create (Customer customer)
        {
             if (customer.Name == null)
            {
                ModelState.AddModelError("Name", "Назва повинна бути заповнена");
            }
             if (ModelState.IsValid)
             {
                 _dataManager.Customers.Insert(customer);
                 return RedirectToAction("List");
             }
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit (int id)
        {
            ViewData.Model = _dataManager.Customers.SelectById(id);
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, Customer customer)
        {
            if (customer.Name == null)
            {
                ModelState.AddModelError("Name", "Назва повинна бути заповнена");
            }
            if (ModelState.IsValid)
            {

                customer.CustomerId = id;
                _dataManager.Customers.Update(customer);
                return RedirectToAction("List");
            }
            return View();
        }

        public ActionResult Delete (int id)
        {
            _dataManager.Customers.Delete(id);
            return RedirectToAction("List");
        }

   }
}
